<!doctype  html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
	<title>图片轮播</title>
<style type="text/css">
       body{
       	   background: url("image/6_bg.png");
       	   background-size: 100% 100%;
       	   background-repeat: no-repeat;
       	   min-height:450px;
       	   width: 100%;
       }
       form{
       	margin: 0 auto;
       	margin-top:120px;
       	width: 500px;
       	position: relative;
       }
       select{
       	width: 150px;
        height: 20px;
       	margin: 0 auto;
       	position: absolute;
       	left: 175px;
       }
       input{
       	   margin-top: 25px;
       	   width: 200px;

       }
       span{
           font-size:14px; 
       	   display: inline-block;
       	   width:75px;
       	   margin-left: 50px;
       }
      
       .button{
       	   margin-top:20px;
       	   height:25px;
       	   width: 70px;
           float: right;
           margin-right: 120px;
       }
       .a{
          margin-top:22px;
           height:25px;
           width: 70px;
           float: right;
       }
       .leftbutton{
       	   margin-left:100px;
       }
       #file{
          display: none;
       }
       #picture,#upload{
           width: 100px;
       }

</style>
</head>
<body>
	<form>
            <select>
		           <option>图片轮播</option>
		      	</select><br>
		     <span class="leftbutton">图片地址：</span><input type="file" name="file" id="file"><input type="text" id="picture"><input type="button" value="上传" id="upload"><br>
         <span class="leftbutton">文章地址：</span><input type="text" name="article" id="article"><br>
		     <input type="button" class="button" value="提交" id="submit">
         <a class="a" href="content_mag.html">返回</a>
	</form>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="../public/js/serverIp.js"></script>
<script type="text/javascript">
     $(function(){

          var array=window.location.href.split("?");
          var substr=array[1].split("&");

          var datastr="type=ID&bool=false&columnId="+substr[1]+"&modelId="+substr[0]+"&id="+substr[3];
          if(substr[2]=="edit"){
              $.post(serverIp+"/Scauaie.com/artical/showAll",datastr,function(data){
                
                   $("#picture").val(data[0].pictureUrl);
                   $("#article").val(data[0].articleUrl);
              },"json");
          }

           
         $("#upload").click(function(){

                $("#file").trigger("click");
                $("#file").change(function(){
                       // alert($("#file")[0].files)   获取文件列表
                       var files=$(this).prop("files");
                       var formdata = new FormData();
                       formdata.append("file",files[0]);
                       var filename=$(this).val();
                       var pictest = /\.jpg$|\.jpeg$|\.gif$|\.png$/i;

                       if(!pictest.test(filename)){
                          alert("文件格式不支持！");
                       }else{
                            $.ajax({
                                type: "post",
                                url: serverIp+"/Scauaie.com/artical/file/upload",
                                data:formdata,
                                dataType:"json",
                                cache: false,  
                                processData: false,  
                                contentType: false,
                                success:function(data){
                                   $("#picture").val(data.result);
                              }
                           })
                       }   
                 })
           })


          function submitTo(url,id){

                var arr=window.location.href.split("?");
                var sub=arr[1].split("&");
                var time="2016/4/14";
                var picture=$("#picture").val();
                var article=$("#article").val();            
                
                var data={
                    "type":"COLUMN",
                    "bool":false,
                    "modelId":sub[0],
                    "columnId":sub[1],
                    "pictureUrl":picture,
                    "articleUrl":article,
                    "id":id,
                    "pictureArticle_id":id            
                };

               if(!picture||!article){
                      alert("请先填写完整");
               }else{
                  $.post(url,{"data":JSON.stringify(data)},function(data){
                        var dataobj=JSON.parse(data);
                        if(dataobj.bool=="true"){
                            if(confirm("发表成功，留在本页面？")){
                               var href=window.location.href;
                               // window.location.href=href;
                            }else{
                               window.location.href="content_mag.html";
                            }
                        }else{
                           alert("发表失败，请重新尝试.")                          
                        } 
                   },"html")
               }
          }


          $("#submit").click(function(){
                if(substr[2]=="edit"){
                    submitTo(serverIp+"/Scauaie.com/artical/edit",substr[3]);
               }else{
                    submitTo(serverIp+"/Scauaie.com/artical/add",null);
               }  
          })

 })
     
</script>
</body>
</html>	